<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Vue计算属性</title>
  <style>
    /* 定义提示框的基础样式 */
    .msg{
      display:inline-block;
      width:160px;
      height:25px;
      border:1px solid #555;
      text-align:center;
      line-height:25px;
    }
    /* 定义提示框在验证通过时的样式 */
    .success{
      border:1px solid green;
      background-color:lightgreen;
      color:green
    }
    /* 定义提示框在验证失败时的样式 */
    .fail{
      border:1px solid red;
      background-color:pink;
      color:red
    }
  </style>
</head>

<body>
  <div id="app">
      <h3>总计: ¥{{total.toFixed(2)}}</h3>
    <ul>
      <li v-for="(item,i) of cart" :key="i">
        {{item.pid}} | {{item.pname}} | ¥{{item.price.toFixed(2)}} | {{item.count}} —— 小计：¥{{(item.price*item.count).toFixed(2)}}
      </li>
    </ul>
    <h3>总计: ¥{{total.toFixed(2)}}</h3>
  </div>
  <!-- 开发环境版本，包含了有帮助的命令行警告 -->
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    var app = new Vue({
      el:"#app",
      data:{
        cart:[
          {pid:1, pname:"华为",price:4455, count:2},
          {pid:2, pname:"小米",price:3455, count:1},
          {pid:3, pname:"OPPO",price:3356, count:3},
        ]
      },
      computed:{ //叫属性，所以用法同属性的用法——不加()
        total(){
          console.log("调用了一次total()");
          var sum=0;
          for(var p of this.cart){
            sum+=p.price*p.count;
          }
          return sum;
        }
      }
    })
  </script>
</body>

</html>